package tongcheng.com.tongcheng.base.main;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseFragment;
import tongcheng.com.tongcheng.base.main.activity.GeRenXinxiActivity;
import tongcheng.com.tongcheng.base.main.activity.MessageActivity;
import tongcheng.com.tongcheng.base.main.activity.SendOrdersActivity;
import tongcheng.com.tongcheng.base.main.activity.SettingActivity;
import tongcheng.com.tongcheng.base.main.activity.WebViewActivity;
import tongcheng.com.tongcheng.base.main.activity.WoDeQianBaoActivity;
import tongcheng.com.tongcheng.base.xinyong.XinYongFenActivity;
import tongcheng.com.tongcheng.utils.GlideApp;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.widght.PhotoPopWindow;

import static android.app.Activity.RESULT_OK;

/**
 * Created by P on 2017/7/12.
 */

public class FragmentPerson extends BaseFragment {
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.iv_touxiang)
    ImageView iv_touxiang;
    @Bind(R.id.person_ll1)
    LinearLayout mPersonLl1;
    @Bind(R.id.person_ll2)
    RelativeLayout mPersonLl2;
    @Bind(R.id.title_bar)
    RelativeLayout mTitleBar;
    @Bind(R.id.line)
    View mLine;
    @Bind(R.id.person_icon)
    ImageView mPersonIcon;
    @Bind(R.id.person_qianbao)
    LinearLayout mPersonQianbao;
    @Bind(R.id.person_setting)
    LinearLayout mPersonSetting;
    @Bind(R.id.person_message)
    LinearLayout mPersonMessage;
    @Bind(R.id.all)
    RelativeLayout mAll;
    @Bind(R.id.person_xinyong)
    Button mPersonXinyong;
    @Bind(R.id.person_name)
    TextView mPersonName;
    @Bind(R.id.person_yue)
    TextView mPersonYue;
    @Bind(R.id.ll_yongHuZhiNan)
    LinearLayout ll_yongHuZhiNan;
    @Bind(R.id.ll_yaoQingHaoYou)
    LinearLayout ll_yaoQingHaoYou;

    private List<LocalMedia> selectList;


    @Override
    public void FragmentStart() {

    }

    @Override
    public Integer setFragmentView() {
        return R.layout.fragment_person;
    }

    @Override
    public void onResume() {
        super.onResume();
        GlideApp.with(this).load(ConStans.BaseUrl_Test_url + Setting.getTouXiang()).into(iv_touxiang);
        GlideApp.with(this).load(ConStans.BaseUrl_Test_url + Setting.getTouXiang()).transform(new CircleCrop()).into(mPersonIcon);//加载头像
        mPersonName.setText(Setting.getUserName()+"");
        mPersonXinyong.setText("信用积分:"+Setting.getXinYongFen()+">");//设置信用积分
        mPersonYue.setText(Setting.getYuE() + "");
    }

    @Override
    public void FragmentCreate(View inflate) {
        ButterKnife.bind(this, inflate);
        settitlebar(mTitleBar);
        mTitleBar.setBackgroundColor(0x00ffffff);
        mBack.setVisibility(View.VISIBLE);
        mTitle.setText("个人中心");
        mTitle.setTextColor(Color.WHITE);
        mLine.setVisibility(View.INVISIBLE);

        GlideApp.with(this).load(ConStans.BaseUrl_Test_url + Setting.getTouXiang()).into(iv_touxiang);
        GlideApp.with(this).load(ConStans.BaseUrl_Test_url + Setting.getTouXiang()).transform(new CircleCrop()).into(mPersonIcon);//加载头像
        mPersonXinyong.setText("信用积分:"+Setting.getXinYongFen()+">");//设置信用积分
        mPersonName.setText(Setting.getUserName()+"");//初始化用户名

        mPersonYue.setText(Setting.getYuE() + "");


        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        mPersonIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startactivity(getContext(), GeRenXinxiActivity.class);
            }
        });


        mPersonQianbao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startactivity(getContext(), WoDeQianBaoActivity.class);
            }
        });

        ll_yaoQingHaoYou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showShare();
            }
        });

        ll_yongHuZhiNan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent xnIntent = new Intent(mContext,WebViewActivity.class);
                int shenfen = Setting.getShenfen();
                if(shenfen == 1){//企业发单
                    xnIntent.putExtra("keyword",11);
                }else if(shenfen == 2){//个人发单
                    xnIntent.putExtra("keyword",9);
                }
                xnIntent.putExtra("title","用户指南");
                startActivity(xnIntent);
            }
        });

        mPersonSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent xnIntent = new Intent(mContext,SettingActivity.class);
                int shenfen = Setting.getShenfen();
                if(shenfen == 1){//企业发单
                    xnIntent.putExtra("yongHuXieYi",4);
                }else if(shenfen == 2){//个人发单
                    xnIntent.putExtra("yongHuXieYi",2);
                }

                mContext.startActivity(xnIntent);

//                startactivity(getContext(), SettingActivity.class);
            }
        });

        mPersonMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startactivity(getContext(), MessageActivity.class);
            }
        });


        mPersonLl2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PhotoPopWindow PhotoPopWindow = new PhotoPopWindow(getContext(), FragmentPerson.this);
                PhotoPopWindow.show(mAll);
            }
        });

        mPersonXinyong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startactivity(getContext(), XinYongFenActivity.class);
            }
        });
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


   /* @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }*/


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    // ShowToast.showToast("我被回调了");
                    for (LocalMedia localMedia : selectList) {
                        Log.e("tag", localMedia.getPath());
                    }
                    Intent intent = new Intent(mContext, SendOrdersActivity.class);
                    intent.putExtra("zhaopian", (Serializable) selectList);
                    startActivity(intent);
                    break;
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
